package com.leoman.plz_p.views.mine;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.leoman.plz_p.R;
import com.leoman.plz_p.adapter.MissionRecordAdapter;
import com.leoman.plz_p.adapter.StudyRecordAdapter;
import com.leoman.plz_p.interfaces.StudyRecordView;
import com.leoman.plz_p.module.bean.MissionBean;
import com.leoman.plz_p.module.bean.StudyRecordBean;
import com.leoman.plz_p.presenter.StudyRecordPresenter;
import com.leoman.plz_p.views.homepage.CourseDetailActivity;
import com.leoman.plz_p.weidget.NavBar;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cn.com.corelib.base.BaseActivity;

/**
 * 学习记录
 */
public class StudyRecordActivity extends BaseActivity<StudyRecordView, StudyRecordPresenter> implements StudyRecordView {

    @Bind(R.id.nav)
    NavBar nav;
    @Bind(R.id.gv_class)
    GridView gvClass;
    @Bind(R.id.tv_record)
    TextView tvRecord;
    @Bind(R.id.tv_mission)
    TextView tvMission;
    @Bind(R.id.tv_text)
    TextView tvText;

    public static Intent getLaunchIntent(Context context) {
        Intent intent = new Intent(context, StudyRecordActivity.class);
        return intent;
    }

    private boolean isStudyRecord = true;

    private StudyRecordAdapter studyRecordAdapter;
    private MissionRecordAdapter missionRecordAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_study_record;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initNavi();
        studyRecordAdapter = new StudyRecordAdapter(getViewContext());
        missionRecordAdapter = new MissionRecordAdapter(getViewContext());
        gvClass.setAdapter(studyRecordAdapter);
        gvClass.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (isStudyRecord) {
                    startActivity(CourseDetailActivity.getLaunchIntent(getViewContext(),studyRecordAdapter.getItem(i).getCourse()));
                } else {
                    startActivity(MissionDetailActivity.getLaunchIntent(getViewContext(),missionRecordAdapter.getItem(i).getName(),missionRecordAdapter.getItem(i).getId()+""));
                }
            }
        });

    }

    @Override
    protected StudyRecordPresenter createPresenter() {
        return new StudyRecordPresenter();
    }

    private void initNavi() {
        nav.setTitle("学习记录");
    }

    @Override
    public void obtainStudyRecord(List<StudyRecordBean> data) {
        if (data!=null&&data.size()>0)
        studyRecordAdapter.replaceAll(data);

    }

    @Override
    public void obtainMissionRecord(List<MissionBean> rows) {
        missionRecordAdapter.replaceAll(rows);

    }



    @OnClick({R.id.tv_record, R.id.tv_mission})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_record:
                tvRecord.setBackgroundResource(R.drawable.record_background_on);
                tvMission.setBackgroundResource(R.drawable.mission_background_off);
                tvRecord.setTextColor(Color.WHITE);
                tvMission.setTextColor(Color.parseColor("#5ab5f9"));
                isStudyRecord = true;
                gvClass.setAdapter(studyRecordAdapter);
                tvText.setText("我的记录");
                break;
            case R.id.tv_mission:
                tvMission.setBackgroundResource(R.drawable.mission_background_on);
                tvRecord.setBackgroundResource(R.drawable.record_background_off);
                tvMission.setTextColor(Color.WHITE);
                tvRecord.setTextColor(Color.parseColor("#5ab5f9"));
                isStudyRecord = false;
                gvClass.setAdapter(missionRecordAdapter);
                tvText.setText("我的任务");
                break;
        }
    }


}